package com.fairytask
{
	import com.fairytask.data.Modulator;
	import com.fairytask.define.ResDefine;
	import com.fairytask.event.BattleEvent;
	import com.fairytask.event.LoadingEvent;
	import com.fairytask.logic.BattleLogic;
	import com.fairytask.logic.SceneLogic;
	import com.fairytask.resource.ResManager;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.scene.BattleScene;
	import com.fairytask.scene.IScene;
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	
	import flash.display.DisplayObject;
	import flash.system.LoaderContext;
	
	import feathers.controls.Button;
	import feathers.themes.AeonDesktopTheme;
	import feathers.themes.MetalWorksMobileTheme;
	
	import starling.display.Sprite;
	import starling.events.Event;

	/**
	 * 游戏入口 
	 * @author hanjy
	 * 
	 */	
	public class GameStage extends Sprite
	{
		public static var instance	:GameStage;
		
		protected var theme:MetalWorksMobileTheme;
		private var button:Button;
		
		public static var ROOTW	:int	=	1000;
		public static var ROOTH	:int	=	600;
		//定义场景层次
		public static var sceneSpace	:Sprite;
		private var uiSpace			:Sprite;
		
		private var queueLoader:QueueLoader;
		private var model:Modulator;
		//是否配置文件
		private var isConfig:Boolean = false;
		private var isAssets:Boolean = false;
		private var isFlashRes:Boolean;
		
		public function GameStage()
		{
			super();
			model	=	Modulator.instance;
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		private function addedToStageHandler(evt:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			this.theme = new MetalWorksMobileTheme(this.stage);
			
			//场景定义
			sceneSpace	=	new Sprite();
			addChild(sceneSpace);
			
			//TODO 开始加载
			var context	:LoaderContext	=	new LoaderContext();
			//context.securityDomain	=	SecurityDomain.currentDomain;
			queueLoader	=	new QueueLoader();
			queueLoader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onLoaded);
			queueLoader.addItem("config/ConfigData.swf");
			queueLoader.execute();
			
			//加载皮肤
			//ResManager.addTexture(ResDefine.MODEL_PATH+"popBasic.png");
			ResManager.addAtlas(ResDefine.MODEL_PATH+"popSheet.png",ResDefine.MODEL_PATH+"popSheet.xml");
			ResManager.loadRes(ResDefine.MODEL_PATH+"Cell.swf",onResLoaded);
			
			//皮肤加载
			model.addEventListener(LoadingEvent.RES_READY,onResReady);
		}
		
		/**
		 * flash资源加载完毕 
		 * @param val
		 * @param pList
		 * 
		 */
		private function onResLoaded(val:DisplayObject,pList:Array=null):void
		{
			isFlashRes	=	true;
			checkReady();
		}
		
		private function checkReady():void
		{
			if(isFlashRes&&isAssets&&isConfig)
			{
				//加载完毕开始逻辑
				initLogic();
			}
		}
		
		/**
		 * 皮肤加载完成 
		 * @param evt
		 * 
		 */
		private function onResReady(evt:LoadingEvent):void
		{
			isAssets	=	true;
			checkReady();
		}
		
		/**
		 * 资源加载完毕 
		 * @param event
		 * 
		 */
		protected function onLoaded(event:QueueLoaderEvent):void
		{
			StaticIni.initStaticFromObj(event.content);
			isConfig	=	true;
			checkReady();
		}
		
		/**
		 * 开始游戏处理逻辑 
		 */
		private function initLogic():void
		{
			BattleLogic.instance;
			SceneLogic.instance;
			//测试用
			model.dispatchEvent(new BattleEvent(BattleEvent.ENTER_BATTLE,100001));
		}
	}
}